


class Monkey{
    constructor(){
        this._humans = [];
    }
    removeHuman(human){
        let idx = this._humans.indexOf(human);
        if(idx >= 0){
            this._humans.splice(idx,1);
        }
    }

    addHuman(human){
        this._humans.push(human);
    }

    // 表演
    act(cmd){
        this._humans.forEach(human=>{
            human.onReceive(cmd);
        });
    }
}


class Human{

    constructor(name){
        this._monkey = null;
        this._name = name;
    }

    viewMonkey(monkey){
        if(this._monkey){
            this._monkey.removeHuman(this);
        }
        this._monkey = monkey;
        if(monkey){
            monkey.addHuman(this);
        }
    }

    onReceive(cmd){
        console.log(this._name,'看到->',cmd);
    }
}

let monkey = new Monkey();
let zhang = new Human('zhangsan');
zhang.viewMonkey(monkey);

let lisi = new Human('lizi');
lisi.viewMonkey(monkey);

monkey.act('胸口碎大石');

